﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SpreadsheetGear;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.InternalServices;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer
{
    /// <summary>
    /// Форма регламентного отчета
    /// </summary>
    public partial class ProceduralReportForm : RestorableForm
    {
        //private Excel _excel;
        private ProceduralReport _report;

        /// <summary>
        /// Строит регламентный отчет
        /// </summary>
        private void Build()
        {
            //BBB Text = Text + " - " + _report.caption;
            ProceduralReportViewer.GetLock();
            IWorksheet sheet = ProceduralReportViewer.ActiveWorksheet;            
            //BBB sheet.Name = _report.caption;
            sheet.Cells["A1:J1"].Columns.Group();
            sheet.Cells["A1:J1"].Font.Size = 15;            
            sheet.Cells["A1:J1"].Style.HorizontalAlignment = HAlign.Center;
            sheet.Cells["A1:J1"].Style.VerticalAlignment = VAlign.Center;
            sheet.Cells["A1:J1"].Borders.LineStyle = LineStyle.Continous;
            //BBB sheet.Cells["E1"].Value = _report.caption;
            sheet.Cells["A1"].ColumnWidth = 30;
            sheet.Cells["A2:J2"].Columns.Group();
            sheet.Cells["A2:J2"].Font.Size = 13;
            sheet.Cells["A2:J2"].Style.HorizontalAlignment = HAlign.Center;
            sheet.Cells["A2:J2"].Style.VerticalAlignment = VAlign.Center;
            sheet.Cells["A2:J2"].Borders.LineStyle = LineStyle.Continous;
            //bbbsheet.Cells["E2"].Value = _report.subcaption1;
            /*bbb
            for (int i = 0; i < _report.rowsCaptions.Count; i++)
            {
                sheet.Cells[i + 3, 0].Value = _report.rowsCaptions[i];
                for (int j = 0; j < _report.columnsCaptions.Count; j++)
                {
                    if (sheet.Cells[2, j + 1].Text == string.Empty)
                    {
                        sheet.Cells[2, j + 1].Value = _report.columnsCaptions[j];
                    }                    
                }
            }
            for (int i = 0; i < _report.values1.Count; i++)
            {
                ProceduralReportValueCell cell = _report.values1[i];
                Point location = cell.location;
                sheet.Cells[location.Y + 3, location.X + 1].Value = cell.value;
            }
            int c = _report.rowsCaptions.Count + 3 + 2;
            sheet.Cells["A" + c.ToString() + ":J" + c.ToString()].Columns.Group();
            sheet.Cells["A" + c.ToString() + ":J" + c.ToString()].Font.Size = 13;
            sheet.Cells["A" + c.ToString() + ":J" + c.ToString()].Style.HorizontalAlignment = HAlign.Center;
            sheet.Cells["A" + c.ToString() + ":J" + c.ToString()].Style.VerticalAlignment = VAlign.Center;
            sheet.Cells["A" + c.ToString() + ":J" + c.ToString()].Borders.LineStyle = LineStyle.Continous;
            //bbbsheet.Cells["E" + c.ToString()].Value = _report.subcaption2;
            for (int i = 0; i < _report.rowsCaptions.Count; i++)
            {
                sheet.Cells[i + 3 + c, 0].Value = _report.rowsCaptions[i];
                for (int j = 0; j < _report.columnsCaptions.Count; j++)
                {
                    if (sheet.Cells[2 + c, j + 1].Text == string.Empty)
                    {
                        sheet.Cells[2 + c, j + 1].Value = _report.columnsCaptions[j];
                    }
                }
            }
            for (int i = 0; i < _report.values2.Count; i++)
            {
                ProceduralReportValueCell cell = _report.values2[i];
                Point location = cell.location;
                sheet.Cells[location.Y + 3 + c, location.X + 1].Value = cell.value;
            }
            ProceduralReportViewer.ReleaseLock();
            //_excel = new Excel();
            //_excel.NewDocument();
            //string fname = Application.LocalUserAppDataPath + "\\" + Resources.PROCEDURALREPORT_TEMPFILE;
            //_excel.SaveDocument(fname);
            //_excel.CloseDocument();
            //SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(fname);    
            //workbook.sa                        */
        }

        private void SaveReport()
        {
            SaveReportDialog.InitialDirectory = Application.LocalUserAppDataPath;
            if (SaveReportDialog.ShowDialog() == DialogResult.OK)
            {
                ProceduralReportViewer.GetLock();
                ProceduralReportViewer.ActiveWorksheet.SaveAs(SaveReportDialog.FileName, FileFormat.Excel8);
                ProceduralReportViewer.ReleaseLock();
            }
        }

        /// <summary>
        /// Конструктор формы
        /// </summary>
        /// <param name="report"></param>
        public ProceduralReportForm(ProceduralReport report)
        {
            _report = report;
            InitializeComponent();
        }
               
        private void ProceduralReportForm_Load(object sender, EventArgs e)
        {
            Build();
        }

        private void SaveReportButton_Click(object sender, EventArgs e)
        {
            SaveReport();
        }
    }
}
